import {Component, OnInit, TemplateRef} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from '../../../../environments/environment';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {
  param: any = {
    firstResult: 0,
    maxResults: 10,
    latestVersion: true
  };

  result: any = {
    data: [],
    count: 0
  };

  constructor(
    private http: HttpClient,
    private message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.load();
  }

  load(): void {
    this.http.get<number>(`${environment.restUrl}/process-definition/count`).subscribe(res => {
      this.result.count = res[`count`];
      if (this.result.count > 0) {
        this.http.get<any[]>(`${environment.restUrl}/process-definition`,
          {params: this.param}).subscribe(res1 => {
          this.result.data = res1;
        });
      } else {
        this.result.data = [];
      }
    });
  }

  changePage(e): void {
    this.param.firstResult = (e - 1) * this.param.maxResults;
    this.load();
  }

  delete(key): void {
    const param: any = {cascade: true, skipCustomListeners: true, skipIoMappings: true};
    this.http.delete(`${environment.restUrl}/process-definition/key/${key}/delete`, param).subscribe(res => {
      this.message.success('删除成功');
      this.load();
    });
  }
}
